﻿
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MS.BasicData.Read.API.Controllers
{
    /// <summary>
    /// Image controller.图片控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ImageController : ControllerBase
    {
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ImageFile(IFormFile file)
        {
            string savePath = Directory.GetCurrentDirectory() + @"/wwwroot/Pic/";

            string newFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);

            //上传图片到服务器
            using (FileStream fs = new FileStream(savePath + newFileName, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }

            //返回图片地址
            return Ok(newFileName);
        }

    }
}
